#!/bin/sh
#
# Brent Smith <gnome@nextreality.net>
# Version: none yet
#
# TODO:
#   process uppercase/lowercase extensions (i.e., jpg|JPG)

# file where we can output any non-fatal errors
UNIXTIME=`date '+%s'`
FILETMP="/tmp/convert.$UNIXTIME"

# check for the convert utility
CONVERT=`which convert`
if [ ! -x "$CONVERT"  ]; then
  zenity --info --title "Missing utility" --text "You do not have the ImageMagick convert utility installed, or it is not in the \$PATH."
  exit 1;
fi

# ask the user for the file extension
FILEEXT=`zenity --list \
                --title="Select the input file extension" \
                --print-column="2" \
                --column="Description" --column="Extension" \
                "JPEG files" "JPG" \
                "PNG files" "png"` 

# check to see if the user cancelled the operation
if [ "$?" -ne "0" ]; then
  zenity --info --title "User cancel" --text "User cancelled the operation"
  exit 1;
fi

# ask user for the output size
SIZE=`zenity --list \
             --title="Select the file type" \
             --print-column="ALL" \
             --column="Width" --column="Height" --column "Output Directory" \
             "640"    "480"    "640x480" \
             "24.69%" "24.69%" "24.69percent"` 

# check to see if the user cancelled the operation
if [ "$?" -ne "0" ]; then
  zenity --info --title "User cancel" --text "User cancelled the operation"
  exit 1;
fi

# the desired width and height of the new images
WIDTH=`echo $SIZE | cut -f1 '-d|'`
HEIGHT=`echo $SIZE | cut -f2 '-d|'`
NEWDIR=`echo $SIZE | cut -f3 '-d|'`

# check to make sure the nautilus variable exists/is not empty
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
  zenity --error --title "Environment not set" --text "Environment variable NAUTILUS_SCRIPT_SELECTED_FILE_PATHS not set or empty"
  exit 1;
fi

# must quote environment variable, so word splitting expansion is not performed
# (linefeeds are kept intact)
# also, nautilus seems to put an extra linefeed at the end of the variable so
# we use sed to delete it
SELECTED_FILES=`echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | sed -e '$d'` 

# we assume the user has selected a bunch of directories
echo "$SELECTED_FILES" | while read IMGDIR
do
  
  if [ -z "$IMGDIR" ]; then
    echo '$IMGDIR is empty, this should not happen.' >> $FILETMP
    continue
  elif [ ! -d "$IMGDIR" ]; then
    echo "$IMGDIR is not a directory.  Please select only directories" >> $FILETMP
    continue
  fi
  
  NEWDIR=${WIDTH}x${HEIGHT}

  mkdir -p "$IMGDIR/$NEWDIR"

  if [ "$?" -ne "0" ]; then
    zenity --error --title "Directory creation failed" --text "Failed to create $IMGDIR/$NEWDIR"
    exit 1;
  fi

  FILECOUNT=`ls -1 "$IMGDIR"/*.$FILEEXT | wc -l`
  CUR=0
  PERCENT=0

  ( for i in "$IMGDIR"/*.$FILEEXT; do
      BASENAME=`basename "$i"`
      echo $PERCENT
      echo "#converting '$i' to ${WIDTH}x${HEIGHT}"
      convert -scale "${WIDTH}x${HEIGHT}" "$i" "$IMGDIR/$NEWDIR/$BASENAME"
      CUR=$[$CUR+1]
      PERCENT=$[(${CUR}*100)/$FILECOUNT]
    done
  ) | zenity --progress \
             --width=640 \
             --height=100 \
             --title "Converting images..." \
             --text "Starting..." \
             --percentage 0 \
             --auto-close

  # check to see if the user cancelled the operation
  if [ "$?" -ne "0" ]; then
    zenity --info --title "User cancel" --text "User cancelled the operation"
    exit 1;
  fi

done

# check for the existence of the temp file with non-fatal errors
if [ -f $FILETMP ]; then
   zenity --width=480 --height=360 --title "Non fatal errors" --text-info --filename=$FILETMP
fi

